package com.qianya.system.manage.web.sys.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qianya.model.pojo.sys.qo.SysParamQO;
import com.qianya.model.pojo.sys.vo.SysParamVO;
import com.qianya.system.manage.config.RedisUtil;
import com.qianya.system.manage.web.login.model.LoginUsers;
import com.qianya.system.manage.web.login.service.ILoginUsersService;
import com.qianya.system.manage.web.sys.model.SysParam;
import com.qianya.system.manage.web.sys.service.ISysParamService;
import com.qianya.system.manage.web.sys.mapper.SysParamMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.qianya.model.common.CommonResult;
import com.qianya.model.common.ResultCode;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* <p>
*  字典服务实现类
* </p>
*
* @author lhl
* @since 2020-06-12
*/
@Slf4j
@Service
public class SysParamServiceImpl extends ServiceImpl<SysParamMapper, SysParam> implements ISysParamService {
    @Autowired
    private ILoginUsersService usersService;

    /**
     * @desc 缓存字典
     * @author lhl
     * @date 2020-07-15 09:13
     * @return
    */
        @Override
        public void paramCache() {
            List<SysParam> sysList=list();
            List<SysParamVO> listSPV=new ArrayList<SysParamVO>();
            SysParamVO sysParamVO=null;
            for (int i = 0; i <sysList.size() ; i++) {
                sysParamVO=new SysParamVO();
                sysParamVO.setPaId(sysList.get(i).getPaId());
                sysParamVO.setPaName(sysList.get(i).getPaName());
                sysParamVO.setPaType(sysList.get(i).getPaType());
                listSPV.add(sysParamVO);
            }
            RedisUtil.set("sysParam",listSPV);
            List<LoginUsers> userList=usersService.list();
            Map<String,Object> map=new HashMap<String, Object>();
            for (int i = 0; i <sysList.size() ; i++) {
                map.put("pa_name"+sysList.get(i).getPaId(),sysList.get(i).getPaName());
            }
            for (int i = 0; i <userList.size() ; i++) {
                map.put("u_username"+userList.get(i).getUId(),userList.get(i).getUUsername());
            }
            if (!map.isEmpty()){
                RedisUtil.hset("paramDictMap",map);
                log.info("字典缓存成功...");
            }
        }

    /**
        *查找 字典
        * @author lhl
        * @since 2020-06-12
        */
        @Override
        public CommonResult find(SysParamQO sysParam) {
            QueryWrapper<SysParam> queryWrapper=new QueryWrapper<SysParam>().eq(StrUtil.isNotBlank(sysParam.getPaType()),"PA_TYPE",sysParam.getPaType());
                queryWrapper.like(StrUtil.isNotBlank(sysParam.getPaName()),"PA_NAME",sysParam.getPaName());
            queryWrapper.orderByAsc("PA_CREATE_TIME");
            return CommonResult.success(list(queryWrapper));
        }
        /**
        * 新增 字典
        * @author lhl
        * @since 2020-06-12
        */
        @Override
        public CommonResult create(SysParam sysParam,HttpServletRequest request) {
            boolean b = save(sysParam);
            if (b){
                Map<String,Object> map=new HashMap<String, Object>();
                map.put("pa_name"+sysParam.getPaId().toString(),sysParam.getPaName());
                RedisUtil.hset("paramDictMap",map);
                return CommonResult.success();
            }
            return CommonResult.error(ResultCode.ERROR,"操作失败");
        }
        /**
        * 修改 字典
        * @author lhl
        * @since 2020-06-12
        */
        @Override
        public CommonResult renewal(SysParam sysParam,HttpServletRequest request) {
            boolean b = updateById(sysParam);
            if (b){
                Map<String,Object> map=new HashMap<String, Object>();
                map.put("pa_name"+sysParam.getPaId().toString(),sysParam.getPaName());
                RedisUtil.hset("paramDictMap",map);
                return CommonResult.success();
            }
            return CommonResult.error(ResultCode.ERROR,"操作失败");
        }

}
